package com.ms.service.module.analysisgroup.db;

import java.math.BigDecimal;
import java.util.ArrayList;

import org.hibernate.Query;
import org.hibernate.Session;

import com.ms.game.db.util.UpdateQueryBuilder;
import com.ms.service.module.analysisgroup.CoinExpenditureWay;
import com.ms.service.module.analysisgroup.db.model.BasicAlertLogModel;
import com.ms.service.module.analysisgroup.db.model.CoinExpenditureAlertSettingModel;
import com.ms.service.module.analysisgroup.db.model.CoinExpenditureLogModel;
import com.ms.service.module.analysisgroup.db.model.GameRevenueAndExpenditureAlertLogModel;
import com.ms.service.module.analysisgroup.db.model.GameRevenueAndExpenditureModel;
import com.ms.service.module.analysisgroup.db.model.ItemChangeCountModel;
import com.ms.service.module.analysisgroup.db.model.UsedCoinCardDetailLogModel;
import com.xpec.c4.db.hibernate.manager.HibernateAnalysisManager;
import com.xpec.c4.game.utility.GameUtility;

public class AnalysisGroupDAO {
	
	private static class AnalysisGroupDAOInstance {
		public static AnalysisGroupDAO instance = new AnalysisGroupDAO();
	}
	public static AnalysisGroupDAO getInstance(){
		return AnalysisGroupDAOInstance.instance;
	}
	
	/**
	 * 儲存遊戲收支紀錄
	 */
	public void saveGameRevenueAndExpenditureLog(GameRevenueAndExpenditureModel model){
		Session session = HibernateAnalysisManager.txBegin();
		model.setRecordTime(GameUtility.getCurrentDate());
		session.save(model);
		HibernateAnalysisManager.txCommitFailRollbackClearSession();
	}
	
	/**
	 * 儲存遊戲收支警示LOG
	 */
	public void saveGameRevenueAndExpenditureAlertLog(GameRevenueAndExpenditureAlertLogModel model){
		Session session = HibernateAnalysisManager.txBegin();
		model.setRecordTime(GameUtility.getCurrentDate());
		model.setHandleStatus(BasicAlertLogModel.HANDLE_STATUS_NEW);
		session.save(model);
		HibernateAnalysisManager.txCommitFailRollbackClearSession();
	}
	
	/**
	 * GMTool處理警示紀錄
	 */
	public boolean updateGMToolHandleAlertLog(String modelName, int recordID, int handleStatus, String handleMemo){
		Session session = HibernateAnalysisManager.txBegin();

		UpdateQueryBuilder builder = new UpdateQueryBuilder();
		builder.setSession(session);
		builder.setTableName(modelName);
		builder.addSetValue("handleStatus", handleStatus);
		builder.addSetValue("handleMemo", handleMemo);
		builder.addWhereKey("recordID", recordID);
		builder.executeQuery();

		return HibernateAnalysisManager.txCommitFailRollbackClearSession();
	}
	
	/**
	 * 取得所有C幣產出警示設定
	 * @return
	 */
	public ArrayList<CoinExpenditureAlertSettingModel> getAllCoinExpenditureAlertSetting(){
		ArrayList<CoinExpenditureAlertSettingModel> result = new ArrayList<CoinExpenditureAlertSettingModel>();
		Session session = HibernateAnalysisManager.txBegin();
		Query query = session.createQuery("from CoinExpenditureAlertSettingModel");
		result = (ArrayList<CoinExpenditureAlertSettingModel>) query.list();
		HibernateAnalysisManager.endSession();
		
		return result;
	}
	
	/**
	 * 新增一個初始的C幣產出警示設定
	 * @param coinExpenditureWay
	 */
	public void insertNewCoinExpenditureSetting(CoinExpenditureWay coinExpenditureWay){
		CoinExpenditureAlertSettingModel NewModel = new CoinExpenditureAlertSettingModel();
		NewModel.setCoinExpenditureWay(coinExpenditureWay);
		NewModel.setCoinExpenditureWayDesc(coinExpenditureWay.getDesc());
		NewModel.setAlertValue(CoinExpenditureAlertSettingModel.DEFAULT_ALERT_VALUE);
		
		Session session = HibernateAnalysisManager.txBegin();
		session.save(NewModel);
		HibernateAnalysisManager.txCommitFailRollbackClearSession();
	}
	
	/**
	 * 儲存C幣支出紀錄
	 */
	public void saveCoinExpenditureLog(CoinExpenditureLogModel model){
		Session session = HibernateAnalysisManager.txBegin();
		model.setRecordTime(GameUtility.getCurrentDate());
		session.save(model);
		HibernateAnalysisManager.txCommitFailRollbackClearSession();
	}
	
	/**
	 * GMTool修改C幣支出警示值
	 */
	public boolean updateCoinExpenditureAlertValue(CoinExpenditureWay coinExpenditureWay, BigDecimal alertValue){
		Session session = HibernateAnalysisManager.txBegin();

		UpdateQueryBuilder builder = new UpdateQueryBuilder();
		builder.setSession(session);
		builder.setTableName("CoinExpenditureAlertSettingModel");
		builder.addSetValue("alertValue", alertValue);
		builder.addWhereKey("coinExpenditureWay", coinExpenditureWay);
		builder.executeQuery();

		return HibernateAnalysisManager.txCommitFailRollbackClearSession();
	}

	/**
	 * 儲存警示LOG
	 */
	public void saveAlertLog(BasicAlertLogModel model){
		Session session = HibernateAnalysisManager.txBegin();
		session.save(model);
		HibernateAnalysisManager.txCommitFailRollbackClearSession();
	}
	
	/**
	 * 儲存全部的ItemChangeCountModel
	 */
	public void saveItemChangeCountModels(ArrayList<ItemChangeCountModel> changeCountModels){
		Session session = HibernateAnalysisManager.txBegin();

		for(ItemChangeCountModel model : changeCountModels){
			session.save(model);
		}

		HibernateAnalysisManager.txCommitFailRollbackClearSession();
	}
	
	/**
	 * 儲存全部的使用C幣卡細節
	 */
	public void saveUsedCoinCardDetailLogModels(ArrayList<UsedCoinCardDetailLogModel> logModels){
		Session session = HibernateAnalysisManager.txBegin();

		for(UsedCoinCardDetailLogModel model : logModels){
			session.save(model);
		}

		HibernateAnalysisManager.txCommitFailRollbackClearSession();
	}
}
